package com.nstar.business
{
	import org.osflash.arp.ServiceLocatorTemplate;
	import org.osflash.arp.AMF0Service;

	/**
	 * 保存后台服务链接信息
	 */
	public class ServiceLocator extends ServiceLocatorTemplate
	{
		//
		// Properties
		//
		private static var inst:ServiceLocator;		// instance of the Service Locator singleton
		private var gatewayURL:String;
		
		// Inherited from template:
		// serviceRegistry (private) - registry of known services
		
		////////////////////////////////////////////////////////////////////////////
		//
		// Constructor
		//
		////////////////////////////////////////////////////////////////////////////
		function ServiceLocator (gatewayURL:String)
		{
			this.gatewayURL = gatewayURL;
			super();
			// debug
			// NetDebug.initialize();
		}
		
		////////////////////////////////////////////////////////////////////////////
		//
		// Public Methods
		//
		////////////////////////////////////////////////////////////////////////////
	
		////////////////////////////////////////////////////////////////////////////
		//
		// getInstance()
		//
		// The ServiceLocator is a singleton and this is its accessor method.
		//
		////////////////////////////////////////////////////////////////////////////
		public static function getInstance (gatewayURL:String = "/helloworldFX/gateway"):ServiceLocator
		{
			if (gatewayURL == "" || gatewayURL == null){
				throw new Error("gatewayURL is null or is empty!");
			}
			
			if ( !inst )
			{
				// create a single instance of the singleton
				inst = new ServiceLocator(gatewayURL);
			}
			return inst;
		}
	
		////////////////////////////////////////////////////////////////////////////
		//
		// Private Methods
		//
		////////////////////////////////////////////////////////////////////////////
	
		////////////////////////////////////////////////////////////////////////////
		//
		// addServices()
		//
		// Override the addServices() primitive operation in the superclass to 
		// add the services used by this application.
		//
		////////////////////////////////////////////////////////////////////////////
		override protected function addServices ():void
		{
			//
			// Add your services here
			//
			//在openAMF里配置的Java服务
			gatewayURL = "http://localhost:8080/helloworldFX/gateway";
			var userServices:AMF0Service = new AMF0Service ( gatewayURL, "userServices", null );
			addService ( "userServices", userServices );

		}
	}
}